// pagesShouYe/pages/xiangCun/index.js
const app = getApp();
import { getStorage } from "../../../utils/localStorage";
import { baseUrl } from "../../../api/request";
import { listVillage } from "../../../api/homePage"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //自定义导航栏组件参数
        navData: {
            level: 2,
            title: '旅游乡村'
        },
        //自定义导航栏高度
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        //图片ip地址
        imgUrl: undefined,
        //乡村列表
        villageList: [],
        //当前位置经纬度
        latLng: undefined,
        //列表查询参数
        listQuery: {
            pageSize: 10,
            pageNum: 1,
            longitude: "",
            latitude: "",
            townShipName: ""
        },
        //列表总页数
        total: 0,
        //上拉加载更多状态
        showPull: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            imgUrl: baseUrl,
            "listQuery.longitude": options.longitude || "",
            "listQuery.latitude": options.latitude || "",
            "listQuery.townShipName": options.townShipName || ""
        });
        this.getVillageList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    //获取乡村列表
    getVillageList() {
        const _this = this.data;
        listVillage(_this.listQuery).then(res => {
            res.data.rows.forEach(item => {
                item.villageImage = item.villageImage ? item.villageImage.split(",") : [];
                item.villageLabel = item.villageLabel ? item.villageLabel.split(",") : [];
            });
            this.setData({
                villageList: [..._this.villageList, ...res.data.rows],
                total: Math.ceil(res.data.total / _this.listQuery.pageSize)
            });
        });
    },

    //列表点击事件
    listTap(e) {
        const item = e.currentTarget.dataset.item;
        wx.navigateTo({
          url: '/pagesShouYe/pages/xaingcunDetail/index?id='+item.villageId,
        });
    },

    //图片点击事件
    imgTap(e) {
        const url = e.currentTarget.dataset.url,
            urlList = e.currentTarget.dataset.urllist;
        console.log(urlList, 'urlList');
        const arr = urlList.map(item => this.data.imgUrl + item);
        app.imagePreview(url, arr);
    },

    //上拉加载更多
    pullUp() {
        let _this = this.data;
        if ((_this.listQuery.pageNum + 1) <= _this.total) {
            this.setData({
                showPull: false,
                "listQuery.pageNum": _this.listQuery.pageNum += 1
            });
            this.getVillageList();
        } else {
            this.setData({
                showPull: true
            });
        };
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})